<?php 
	
	//配置文件
	define('HOST','localhost');
	define('USER','root');
	define('PASSWD','');
	define('DBNAME','test');

	/**自定义model
	*
	*/
	class Model
	{
		protected $link = null;//数据库连接对象
		protected $tabname;//检索的表名
		protected $pk = 'uid';//主键
		protected $field = [];//存储字段信息

		

		
		//1 构造方法 连接数据库 选择字符集 设置字符集
		public function __Construct($tabname)
		{
			$this->link=mysqli_connect(HOST,USER,PASSWD) or die("数据库连接失败");
			mysqli_set_charset($this->link,'utf8');//设置字符集
			mysqli_select_db($this->link,DBNAME);//选择数据库
			$this->tabname=$tabname;//选择表名
			$this->field=$this->desc();//获得字段信息

		}

		//2 自定义的方法 数据库的增删改查搜索和分页
		//=======================数据库的增删改查、搜索和分页============================

		//1 查看数据库里全部信息
		public function findAll()
		{
			//1 执行sql查询,获得结果集
			$sql = "select * from {$this->tabname}";
			$res = mysqli_query($this->link,$sql);
			//2解析结果集
			if(mysqli_num_rows($res)>0){
				return  mysqli_fetch_all($res,MYSQLI_ASSOC);
			}else{
				return "数据库查询失败";
			}

		}

		//2 查看某条记录
		public function find($uid=1)
		{
			//1执行sql查询,获得结果集
			$sql = "select * from {$this->tabname} where {$this->pk}={$uid}";
			$res = mysqli_query($this->link,$sql);
			//2解析结果集
			if(mysqli_num_rows($res)>0){
				$list = mysqli_fetch_all($res,MYSQLI_ASSOC);
				return $list;
			}else{
				return "未查到相应数据";
			}
		}

		//3 删除某条记录
		public function del($uid=0)
		{
			//1执行sql
			$sql = "delete from {$this->tabname} where {$this->pk}={$uid}";
			mysqli_query($this->link,$sql);
			if (mysqli_affected_rows($this->link)>0) {
				return mysqli_affected_rows($this->link);
			}else{
				return "删除失败";
			}
		}

		//4 添加记录
		
		//获得表的字段信息


		private function desc()
		{
			// //1 执行sql语句获得结果集
			$sql = "desc {$this->tabname}";
			$res = mysqli_query($this->link,$sql);
			$row = mysqli_fetch_all($res,MYSQLI_ASSOC);
			for($i=0;$i<count($row);$i++){
                $field[]=$row[$i]['Field'];
            }

            $this->field=$field;

            return $field;
		}


		public function store()
		{
			//1 存储有效的字段和值
			$fieldArray = [];
			$valueArray = [];
			
			//2 验证信息的有效
			foreach($_POST as $k=>$v){
				if(in_array($k,$this->field)){//判断提交的数据下标 是否在数据库的字段中
					$fieldArray[$k]= $k;//存下标
					$valueArray[$k]= $v;//存值
				}
			}

			//3 拼接sql语句,执行sql语句
			$sql = "insert into {$this->tabname}(".implode(",",$fieldArray).") values('".implode("','",$valueArray)."')";
			// echo $sql;die;Stu
			mysqli_query($this->link,$sql);
			//4 自增id号
			return "添加成功UID为:".mysqli_insert_id($this->link);
		}

		//更新数据
		public function replaceInfo($param,$id=0)
		{
			//1 拼接sql语句
			$sql = "update {$this->tabname} set class='{$param}' where {$this->pk}={$id}";
			// echo $sql;die;
			mysqli_query($this->link,$sql);
			if(mysqli_affected_rows($this->link)>0){
				return mysqli_affected_rows($this->link);
			}else{
				return mysqli_error($this->link);
			}
		}

		public function __destruct()
		{
			//判断连接是否存在 关闭数据库
			if($this->link!='null'){
				mysqli_close($this->link);
			}
		}

		
	}
